package com.peng.controller;

import com.peng.entities.Pay;
import com.peng.entities.PayDTO;
import com.peng.result.ResultData;
import com.peng.result.ReturnCodeEnum;
import com.peng.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "支付微服务模块", description = "支付交易表相关接口")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping("/pay/add")
    @Operation(summary = "新增支付交易表", description = "新增支付交易表,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay);
        int add = payService.add(pay);
        return add > 1
                ? ResultData.success("成功插入记录，返回值：" + add)
                : ResultData.fail(ReturnCodeEnum.RC999);
    }
    @DeleteMapping("/pay/del/{id}")
    @Operation(summary = "删除支付交易表", description = "删除支付交易表")
    public ResultData<String> deletePay(@PathVariable("id") Integer id) {
        int del = payService.delete(id);
        return del > 1
                ? ResultData.success("成功删除记录，返回值：" + del)
                : ResultData.fail(ReturnCodeEnum.RC999);

    }
    @PutMapping("/pay/update")
    @Operation(summary = "修改支付交易表", description = "修改支付交易表")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);

        int update = payService.update(pay);
        return update > 1
                ? ResultData.success("成功插入记录，返回值：" + update)
                : ResultData.fail(ReturnCodeEnum.RC999);
    }
    @GetMapping("/pay/get/{id}")
    @Operation(summary = "根据ID查询支付交易表", description = "根据ID查询支付交易表")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        if(id<0)throw new RuntimeException("id不能小于0");
        Pay pay = payService.getById(2);
        return pay != null
                ? ResultData.success(pay)
                : ResultData.fail(ReturnCodeEnum.RC999);
    }
    @GetMapping("/pay/get/all")
    @Operation(summary = "查询所有支付交易表", description = "查询所有支付交易表")
    public ResultData<List<Pay>> getAll() {
        List<Pay> listPay =  payService.getAll();
        return listPay != null
                ? ResultData.success(listPay)
                : ResultData.fail(ReturnCodeEnum.RC999);
    }
}
